<template>
  <view>
    <view class="search-box">
      <my-search
        bgcolor="skyblue"
        title="搜你喜欢~~~"
        radius="5rpx"
        @click.native="goToSearch"
      />
    </view>
    <!-- 轮播图 -->
    <swiper
      class="top-swiper"
      indicator-dots
      autoplay
      circular
      :interval="1000"
      :duration="1000"
    >
      <swiper-item
        class="top-swiper"
        v-for="(item, index) in swiperList"
        :key="index"
      >
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-content"
        >
          <image :src="item.image_src" />
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="handleSkip(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>

        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToProductList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx > 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="goToProductList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badge from '@/mixins/badge'
export default {
  mixins: [badge],
  data() {
    return {
      // 1. 准备数据
      swiperList: [],
      navList: [],
      // 楼层数据
      floorList: [],
    }
  },
  created() {
    this.getSwiperList()
    this.getNavList(), this.getFloorList()
  },
  methods: {
    // 请求轮播图
    async getSwiperList() {
      const { meta, message } = await this.$http.get('/home/swiperdata')
      // 请求失败处理
      if (meta.status !== 200) {
        // uni.showToast({
        //   title: "请求数据失败",
        //   duration: 1500,
        //   icon: "none",
        // })
        return this.$msg()
      }
      // this.$msg('牛哔')
      this.swiperList = message
    },
    // 获取分类导航数据的数据
    async getNavList() {
      const { meta, message } = await this.$http.get('/home/catitems')
      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }
      this.navList = message
    },
    // 跳转到分页
    handleSkip(item) {
      // console.log(item)
      if (item.name === '分类') {
        uni.switchTab({
          url: '/pages/category/category',
        })
      }
    },
    // 获取楼层数据的数据
    async getFloorList() {
      const { meta, message } = await this.$http.get('/home/floordata')
      if (meta.status !== 200) {
        return this.$msg()
      }
      this.floorList = message
    },
    // 跳转到商品列表页（传递参数）
    goToProductList(product) {
      // console.log(product)
      // 从后端返回字段中提取出需要的跳转参数
      const str = product.navigator_url.split('?')[1]
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?' + str,
      })
    },
    // 搜索跳转至搜索页面
    goToSearch() {
      // console.log('跳跳跳跳跳跳过来了')
      uni.navigateTo({
        url: `/pages_sub1/search/search`,
      })
    },
  },
}
</script>

<style lang="scss">
.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}
.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}

.search-box {
  // 设置定位方式
  position: sticky;

  // 设置开始固定定位的条件
  top: 0;

  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}
</style>
